//
//  Language.swift
//  JT-Architecture
//
//  Created by 果咱 on 2020/12/14.
//

import Foundation

typealias LanguageDescription = String

let ENLanguageKey = "en-US"

public enum LanguageType: String, CaseIterable {
    
    case zh = "zh-Hans" //语言文件的文件名
    case en = "en"
    case th = "th"

    static var current: LanguageType { return Language.current.type }
     
    var language: Language { return Language(type: self) }
    
    //语言文件的文件名
    var fileName: String { return rawValue }
    
    //传给服务端的语言value
    var requestKey: LanguageDescription {
        switch self {
        case .zh: return "zh"
        case .en: return "en"
        case .th: return "th"
        }
    }
    
    //展示名称
    var displayName: String {
        switch self {
        case .zh: return "中文"
        case .en: return "English"
        case .th: return "ภาษาไทย"
        }
    }
}

public class Language: Equatable {
    
    public let type: LanguageType
    public var fileName: String { return type.fileName }
    public var displayName: String { return type.displayName }
    public var requestKey: String { return type.requestKey }
    
    public static func `is`(_ type: LanguageType) -> Bool {
        return current.is(type)
    }

    public static var currentType: LanguageType { return current.type }
    
    private func `is`(_ type: LanguageType) -> Bool {
        return self.type == type
    }

    public static var current: Language {
        if let type = Localizator.updatedLanguageType {
            return type.language
        } else if let name = Bundle.main.preferredLocalizations.first, let type = LanguageType(rawValue: name) {
            return type.language
        } else {
            return Language(type: .en)
        }
    }
     
    public init(type: LanguageType) {
        self.type = type
    }
    
    public static func == (lhs: Language, rhs: Language) -> Bool {
        return lhs.type == rhs.type
    }
    
    public static func configure() {
        _ = Language(type: .zh)
    }
}
